package com.yisen.dasan.controller;

import com.alibaba.fastjson.JSONObject;
import com.yisen.dasan.config.Auth;
import com.yisen.dasan.dao.FormMapper;
import com.yisen.dasan.entity.SendAnswer;
import com.yisen.dasan.pojo.AnswerResultSign;
import com.yisen.dasan.service.AnswerService;
import com.yisen.dasan.util.IpUtils;
import com.yisen.dasan.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author yu258234
 * @Description: 填写答案
 */
@RestController
@CrossOrigin
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    AnswerService answerService;
    @Autowired
    FormMapper formMapper;
    @PostMapping("/add")
    public ResultData addAnswer(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        //jsonObject.put("index", IpUtils.getIpAddr(request));
        int formId=jsonObject.getInteger("form_id");
        Map state=formMapper.getState(formId);
        String flag=state.get("state").toString();
        System.out.println("State:::::::::"+flag);
        if (flag.equals("1")){
            boolean b=answerService.addAnswer(jsonObject);
            if(b)
                return new ResultData(true, 200, "添加成功！", jsonObject);
        }

        return new ResultData(false, 200, "添加失败！", state);
    }
//    @GetMapping("/select")
//    public ResultData selectAnswer(@RequestParam("form_id") int formId){
//        List<SendAnswer> list=answerService.getAnswer(formId);
//        return new ResultData(true, 200, "查询成功" , list);
//    }
    @GetMapping("/select")
    @Auth(type = 1)
    public ResultData selectAnswer(@RequestParam("form_id") int formId,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum){
        JSONObject answer = answerService.getAnswer(formId, pageNum);
        return new ResultData(true, 200, "查询成功！", answer);
    }
    @GetMapping("/excelDownload")
    @Auth(type = 1)
    public void writeCsv(@RequestParam("formId") int formId, HttpServletResponse response) throws IOException {
        answerService.getAllAnswer(formId, response);
    }
}
